package com.google.android.apps.iosched.ui;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.android.apps.iosched.R;
import com.google.android.apps.iosched.util.AccountUtils;
import com.google.android.apps.iosched.util.BeamUtils;
import com.google.android.apps.iosched.util.UIUtils;

import java.net.URI;

/**
 * Created by Alex on 13-11-29.
 */
public class BaseActivity extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EasyTracker.getTracker().setContext(this);

        if (!UIUtils.isGoogleTv(this)){
            if(!AccountUtils.isAuthenticated(this)){
                AccountUtils.startAuthenticationFlow(this, getIntent());
                finish();
            }
        }

        if(UIUtils.hasICS()) {
            BeamUtils.setDefaultBeamUri(this);
            if (!BeamUtils.isBeamUnlocked(this)){

                BeamUtils.setBeamCompleteCallback(this,new NfcAdapter.OnNdefPushCompleteCallback() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
                    public void onNdefPushComplete(NfcEvent nfcEvent) {
                        onBeamSent();
                    }
                });
            }
        }

        getSupportActionBar().setHomeButtonEnabled(true);
    }

    private void onBeamSent(){
        if (! BeamUtils.isBeamUnlocked(this)){
            BeamUtils.setBeamUnlocked(this);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new AlertDialog.Builder(BaseActivity.this)
                            .setTitle(R.string.just_beamed)
                            .setMessage(R.string.beam_unlocked_default)
                            .setNegativeButton(R.string.close,null)
                            .setPositiveButton(R.string.view_beam_session,
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            BeamUtils.launchBeamSession(BaseActivity.this);
                                            dialogInterface.dismiss();
                                        }
                                    }).create().show();
                }
            });
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                if(this instanceof HomeActivity){
                    return false;
                }
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    protected void setActionBarColor(int color){
        if (color == 0){
            color = 0xffffffff;
        }

        final Resources res = getResources();
        Drawable maskDrawable = res.getDrawable(R.drawable.actionbar_icon_mask);
        if (!(maskDrawable instanceof BitmapDrawable)){
            return ;
        }

        //获取Bitmap位图对象
        Bitmap maskBitmap = ((BitmapDrawable) maskDrawable).getBitmap();
        final int width = maskBitmap.getWidth();
        final int height = maskBitmap.getHeight();

        //创建一个新的Bitmap位图对象
        Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outBitmap);
        canvas.drawBitmap(maskBitmap,0,0,null);

        Paint maskedPaint = new Paint();
        maskedPaint.setColor(color);
        maskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

        canvas.drawRect(0,0,width,height,maskedPaint);

        BitmapDrawable outDrawable = new BitmapDrawable(res, outBitmap);
        getSupportActionBar().setIcon(outDrawable);
    }

    public static Bundle intentToFragmentArguments(Intent intent){
        Bundle arguments = new Bundle();
        if (intent == null){
            return arguments;
        }

        final Uri data = intent.getData();
        if ( data != null){
            arguments.putParcelable("_uri",data);
        }

        final Bundle extras = intent.getExtras();
        if (extras != null){
            arguments.putAll(intent.getExtras());
        }
        return arguments;
    }

    public static Intent fragmentArgumentsToIntent(Bundle arguments){
        Intent intent = new Intent();
        if (arguments == null){
            return intent;
        }

        final  Uri data = arguments.getParcelable("_uri");
        if (data != null){
            intent.setData(data);
        }

        intent.putExtras(arguments);
        intent.removeExtra("_uri");
        return intent;
    }


    @Override
    protected void onStart() {
        super.onStart();
        EasyTracker.getTracker().trackActivityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EasyTracker.getTracker().trackActivityStop(this);
    }
}
